package sk.adresa.eaukcia.frontend.beans;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;


import org.apache.log4j.Logger;

import org.richfaces.component.UIDataTable;
import org.richfaces.component.html.HtmlDataTable;

import sk.adresa.eaukcia.core.data.Auction;
import sk.adresa.eaukcia.core.query.Paging;
import sk.adresa.eaukcia.core.data.filter.AuctionFilter;
import sk.adresa.eaukcia.core.query.PaginatedList;
import sk.adresa.eaukcia.core.service.AuctionService;
import sk.adresa.eaukcia.frontend.uidata.UiAuction;

public class AuctionBean {


    private Auction auction = new Auction();
    private int scrollerPage;
    private List<UiAuction> uiAuctions;
    private AuctionService auctionService;
    static Logger logger = Logger.getLogger(AuctionBean.class);
    private AuctionLogBean auctionLogBean;
    private UIDataTable table;

    public void setAuctionService(AuctionService auctionService) {
        this.auctionService = auctionService;
    }

    
    public List<UiAuction> getAuctionList() {
        Paging paging = new Paging();
        AuctionFilter filter = new AuctionFilter();

        PaginatedList<Auction> auctionList = auctionService.getFilteredAuctions(filter, paging);
        uiAuctions = new ArrayList<UiAuction>(auctionList.getData().size());
        for (Auction auction : auctionList.getData()) {
            UiAuction uiAuction = new UiAuction(auction);
            uiAuctions.add(uiAuction);
        }
        return uiAuctions;
    }
    
    public String prepareOpeningLog(){
        auctionLogBean.setAuction(auction);
        return "showAuctionLog";
    }
    
    public String prepareOpeningLog1(){
        auctionLogBean.setAuction(auction);
        ChartBean.cc = auctionLogBean.getCeny();
        return "showGraphlog";
    }

    
    public void setScrollerPage(int scrollerPage) {
        this.scrollerPage = scrollerPage;
    }

    public int getScrollerPage() {
        return scrollerPage;
    }


    public void setAuctionLogBean(AuctionLogBean auctionLogBean) {
        this.auctionLogBean = auctionLogBean;
    }

    public AuctionLogBean getAuctionLogBean() {
        return auctionLogBean;
    }

    public void selectionChanged(ActionEvent event) {
        UIComponent comp = event.getComponent();
        Object obj = comp.getParent();
        if (obj instanceof HtmlDataTable) {
            Object rowData = table.getRowData();
            if (rowData instanceof Auction) {
                Auction selObj = (Auction) rowData;
                auction = selObj;
            }
        }
    }
    
    public Auction getAuction(){
        return auction;
    }
    
    public void setTable(UIDataTable table) {
        this.table = table;
    }

    public UIDataTable getTable() {
        return table;
    }

}
